package com.tboss.template.system.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.tboss.global.utils.useful.SecurityUtils;
import com.tboss.template.common.dto.NavigationDto;
import com.tboss.template.common.utils.DateUtils;
import com.tboss.template.system.dao.FunctionDao;
import com.tboss.template.system.dao.GroupDao;
import com.tboss.template.system.dao.RoleDao;
import com.tboss.template.system.dto.GroupDto;
import com.tboss.template.system.dto.MapGrpFnDto;
import com.tboss.template.system.dto.RoleDto;

@Service
public class GroupService {

	@Autowired SystemService systemService;
	@Autowired private MappingGroupFunctionService mapGrpFnService;
	@Autowired private MappingUserInfoGroupService mapUsrInfoGrpService;
	@Autowired private GroupDao grpDao;
	@Autowired private FunctionDao fnDao;
	@Autowired private RoleDao roleDao;
	
	public Integer getGrpListCnt(GroupDto dto){
		return this.grpDao.getGrpListCnt(dto);
	}
	
	public List<GroupDto> getGrpList(GroupDto dto){
		return this.grpDao.getGrpList(dto);
	}
	
	public void setModelGrpList(Integer reqPage, Model model, GroupDto dto){
		if(reqPage != null) dto.setCurrentPage(reqPage);
		
		Integer rowsCnt = this.getGrpListCnt(dto);
		NavigationDto pageNavi = new NavigationDto(rowsCnt, dto.getRowPerPage(), dto.getCurrentPage());
		
		model.addAttribute("sysBase", this.systemService.getSysBase(dto));
		model.addAttribute("pageNavi", pageNavi);
		model.addAttribute("grpList", this.getGrpList(dto));
	}
	
	public void grpRegForm(Model model){
		RoleDto dto = new RoleDto();
		dto.setPagingYn("N");
		model.addAttribute("roleList", this.roleDao.getRoleList(dto));
	}
	
	public void grpRegForm(Model model, String grpId){
		RoleDto dto = new RoleDto();
		dto.setPagingYn("N");
		model.addAttribute("roleList", this.roleDao.getRoleList(dto));
		model.addAttribute("grpInfo", this.grpDao.getGrpInfo(grpId));
	}
	
	public void grpMembers(Model model, String grpId){
		model.addAttribute("memberList", this.grpDao.grpMembers(grpId));
	}
	
	public void addGrpInfo(GroupDto dto){
		dto.setGrpRegId(SecurityUtils.getCurrentUserName());
		dto.setGrpRegDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.grpDao.addGrpInfo(dto);
	}
	
	public void updateGrpInfo(GroupDto dto){
		dto.setGrpModId(SecurityUtils.getCurrentUserName());
		dto.setGrpModDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.grpDao.updateGrpInfo(dto);
		this.grpDao.updateGrpChgChkField(dto.getGrpId());
		
		String compulsoryYN = dto.getGrpCompulsoryYN();
		if(compulsoryYN.equals("Y")){ this.grpDao.addMembGrpCompulsory(dto);			
		}else if(compulsoryYN.equals("N")){ this.grpDao.deleteMembGrpCompulsory(dto);
		}
	}
	
	public void groupFnMgrForm(Model model, String grpId, GroupDto sysDto){
		MapGrpFnDto dto = new MapGrpFnDto();
		dto.setGrpId(grpId);
		List<MapGrpFnDto> list = this.mapGrpFnService.getMappingGrpFnList(dto);
		
		try {
			Map<String, List<MapGrpFnDto>> menu = new HashMap<String, List<MapGrpFnDto>>();
			menu.put(grpId, list);
			
			model.addAttribute("sysBase", this.systemService.getSysBase(sysDto));
			model.addAttribute("mappingList", new ObjectMapper().writeValueAsString(menu).toString());
			model.addAttribute("grpFnMgrId", grpId);
			model.addAttribute("notMappingList", mapGrpFnService.getNotMappingGrpFnList(grpId));
			
		} catch (JsonGenerationException e) { e.printStackTrace();
		} catch (JsonMappingException e) { e.printStackTrace();
		} catch (IOException e) { e.printStackTrace();
		} catch (Exception e) { e.printStackTrace();
		}
	}
	
	public void groupFnMgrFormSave(String grpId, MapGrpFnDto items){
		this.mapGrpFnService.delMappingGrpFn(grpId);
		this.mapGrpFnService.addMappingGrpFn(items);
		this.grpDao.updateGrpChgChkField(grpId);
	}
	
	public void groupFnMgrFormView(Model model, String fnId, GroupDto dto){
		model.addAttribute("sysBase", this.systemService.getSysBase(dto));
		model.addAttribute("fnInfo", this.fnDao.getFnInfo(fnId));
	}
	
}
